﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace AplicacaoReserva
{
    public partial class MDIPrincipal : Form
    {

        public static String nomeAgencia = "";
        public static int idAgencia;

        #region Funcionalidades de Menu
        
        public MDIPrincipal()
        {
            InitializeComponent();
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void logInTSMI_Click(object sender, EventArgs e)
        {
            AgenciaForms.LogInForm logIn = new AgenciaForms.LogInForm();
            logIn.MdiParent = this;
            logIn.Show();
        }

        private void logOutTSMI_Click(object sender, EventArgs e)
        {
            LoggingOut();
        }

        private void editarAgenciaTSMI_Click(object sender, EventArgs e)
        {
            AgenciaForms.EditAgenciaForm edit = new AgenciaForms.EditAgenciaForm();
            edit.MdiParent = this;
            edit.Show();
        }

        private void alterarCodAgenciaTSMI_Click(object sender, EventArgs e)
        {
            AgenciaForms.ChangeCodAgenciaForm change = new AgenciaForms.ChangeCodAgenciaForm();
            change.MdiParent = this;
            change.Show();
        }

        private void criarClientesTSMI_Click(object sender, EventArgs e)
        {
            ClientesForms.CriarClienteForm newClient = new ClientesForms.CriarClienteForm();
            newClient.MdiParent = this;
            newClient.Show();
        }

        private void listarClientesTSMI_Click(object sender, EventArgs e)
        {
            ClientesForms.ListarClientesForm listClients = new ClientesForms.ListarClientesForm();
            listClients.MdiParent = this;
            listClients.Show();
        }

        private void criarReservasTSMI_Click(object sender, EventArgs e)
        {
            ReservasForms.CriarReservaForm newReserva = new ReservasForms.CriarReservaForm();
            newReserva.idAgencia = idAgencia;
            newReserva.MdiParent = this;
            newReserva.Show();
        }

        private void listarReservasTSMI_Click(object sender, EventArgs e)
        {
            ReservasForms.ListarReservasForm listReservas = new ReservasForms.ListarReservasForm();
            listReservas.MdiParent = this;
            listReservas.AgenciaID = idAgencia;
            listReservas.Show();
        }

        private void consultarContactosTSMI_Click(object sender, EventArgs e)
        {
            ContactosForms.ListarContactosForm listcontactos = new ContactosForms.ListarContactosForm();
            listcontactos.MdiParent = this;
            listcontactos.IDAgencia = idAgencia;
            listcontactos.Show();
        }

        private void sobreTSMI_Click(object sender, EventArgs e)
        {
            //Criação de uma janela nova com o titulo
            Form about = new Form();
            about.Height = 130;
            about.Width = 200;
            about.Text = "Realizado por";
            about.MdiParent = this;

            //Criação de uma label com texto inserido
            Label aboutlabel = new Label();
            aboutlabel.Text = "Realizado por:\n\n"
                + "- Denis Barros\n"
                + "- Dilvane Zanardine\n"
                + "- Flávio Pedro\n"
                + "- Jorge Miguel";

            //Formatação da label
            aboutlabel.Height = 230;

            //Adicionando a label à janela
            about.Controls.Add(aboutlabel);

            //Mostrar a janela
            about.Show();
        }

        #endregion

        public void LoggingIn(String nome) {
            try
            {
                AgenciaServiceReference.WCFAgenciaClient agencias = new AgenciaServiceReference.WCFAgenciaClient();

                AgenciaServiceReference.Agencia agencia = agencias.getByCodAgencia(nome);

                agencias.Close();

                nomeAgencia = agencia.nome;
                idAgencia = agencia.IDAgencia;

                FunctionsAtLogIn();
                tss_Status.Text = "Logado como: " + nomeAgencia;
            }
            catch (Exception ex) {
                MessageBox.Show("Cód. Agência inválido!");
                Debug.WriteLine(ex.ToString());
            }
        }


        private void FunctionsAtLogIn() {
            agenciaMenuLogIn();
            clientesMenuLogIn();
            reservasMenuLogIn();
            contactosMenuLogIn();
        }

        private void agenciaMenuLogIn() {
            logInTSMI.Enabled = false;
            logOutTSMI.Enabled = true;
            editarAgenciaTSMI.Enabled = true;
            alterarCodAgenciaTSMI.Enabled = true;
        }

        private void clientesMenuLogIn() {
            ClientesMenu.Enabled = true;
        }

        private void reservasMenuLogIn() {
            ReservasMenu.Enabled = true;
        }

        private void contactosMenuLogIn() {
            ContactosMenu.Enabled = true;
        }


        public void LoggingOut() {
            FunctionsAtLogOut();
            tss_Status.Text = "Deslogado";
            nomeAgencia = "";
        }

        private void FunctionsAtLogOut() {
            agenciaMenuLogOut();
            clientesMenuLogOut();
            reservasMenuLogOut();
            contactosMenuLogOut();
        }

        private void agenciaMenuLogOut()
        {
            logInTSMI.Enabled = true;
            logOutTSMI.Enabled = false;
            editarAgenciaTSMI.Enabled = false;
            alterarCodAgenciaTSMI.Enabled = false;
        }

        private void clientesMenuLogOut()
        {
            ClientesMenu.Enabled = false;
        }

        private void reservasMenuLogOut()
        {
            ReservasMenu.Enabled = false;
        }

        private void contactosMenuLogOut()
        {
            ContactosMenu.Enabled = false;
        }
  
    }
}
